"""Evaluation model for the trading platform."""

from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Boolean, Enum, DECIMAL, JSON, Index
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from database import Base


class Evaluation(Base):
    """Evaluation model representing user evaluations in trades."""
    
    __tablename__ = 'evaluations'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    trade_id = Column(Integer, ForeignKey('trades.id'), nullable=False)
    evaluator_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    evaluated_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    evaluation_type = Column(Enum('buyer_to_seller', 'seller_to_buyer'), nullable=False)
    product_quality_score = Column(Integer)
    service_attitude_score = Column(Integer)
    logistics_speed_score = Column(Integer)
    overall_score = Column(DECIMAL(3, 2), nullable=False)
    content = Column(Text)
    images = Column(JSON)
    tags = Column(JSON)
    is_anonymous = Column(Boolean, default=False)
    is_deleted = Column(Boolean, default=False)
    status = Column(Enum('pending', 'approved', 'rejected'), default='pending')
    admin_remark = Column(String(255))
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
    
    # Relationships
    trade = relationship("Trade", foreign_keys=[trade_id])
    evaluator = relationship("User", foreign_keys=[evaluator_id])
    evaluated = relationship("User", foreign_keys=[evaluated_id])
    
    # Indexes
    __table_args__ = (
        Index('idx_evaluation_trade_id', 'trade_id'),
        Index('idx_evaluator_id', 'evaluator_id'),
        Index('idx_evaluated_id', 'evaluated_id'),
        Index('idx_overall_score', 'overall_score'),
        Index('idx_evaluation_status', 'status'),
    )


class Review(Base):
    """Review model representing user reviews for products."""
    
    __tablename__ = 'reviews'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    product_id = Column(Integer, ForeignKey('products.id'), nullable=False)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    rating = Column(Integer, nullable=False)
    content = Column(Text)
    images = Column(JSON)
    is_approved = Column(Boolean)
    created_at = Column(DateTime, default=func.now())
    
    # Relationships
    product = relationship("Product")
    user = relationship("User")
    
    # Indexes
    __table_args__ = (
        Index('idx_review_product_id', 'product_id'),
        Index('idx_review_user_id', 'user_id'),
    )


class Rating(Base):
    """Rating model representing user ratings for products."""
    
    __tablename__ = 'ratings'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    product_id = Column(Integer, ForeignKey('products.id'), nullable=False)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    rating = Column(Integer, nullable=False)
    created_at = Column(DateTime, default=func.now())
    
    # Relationships
    product = relationship("Product")
    user = relationship("User")
    
    # Indexes
    __table_args__ = (
        Index('idx_rating_product_id', 'product_id'),
        Index('idx_rating_user_id', 'user_id'),
    )